from XEdu.hub import Workflow as wf
import numpy as np
import cv2
#from playsound import playsound
import pygame
import math

flag = 0
text = ' '
num = 1
sound_no = 'kong.wav'
sound_ok = 'kong.wav'
pygame.mixer.init()
flag_num = 2
body = wf(task='body26' )
det_body = wf(task='det_body')
label = ['Hunchback','Upright','Nobody']

def pose(pic):
    result = body.inference(data=pic)
    x1 = result[19][0]
    y1 = result[19][1]
    x2 = result[17][0]
    y2 = result[17][1]
    x3 = result[18][0]
    y3 = result[18][1]
    AB = (x2 - x1, y2 - y1)
    AC = (x3 - x1, y3 - y1)

# 计算点积
    dot_product = AB[0]*AC[0] + AB[1]*AC[1]

# 计算模长
    modulus_AB = math.sqrt(AB[0]**2 + AB[1]**2)
    modulus_AC = math.sqrt(AC[0]**2 + AC[1]**2)

# 计算夹角的余弦值
    cos_theta = dot_product / (modulus_AB * modulus_AC)

# 计算夹角的弧度
    theta_radians = math.acos(cos_theta)

# 将弧度转换为度
    theta_degrees = math.degrees(theta_radians)
    print(theta_degrees)
    if theta_degrees <6:
        num = 1
    else:
        num = 0
    return(num)

def play_sound(num,no,ok):
    if num == 0:
        pygame.mixer.music.load(no)
    elif num == 1:
        pygame.mixer.music.load(ok)
    pygame.mixer.music.play()

body = wf(task='body26' )
det_body = wf(task='det_body')

cap = cv2.VideoCapture(0)
while True:
    ret,img = cap.read()
    if ret:
        
        det,det_box = det_body.inference(data=img,img_type='cv2') # 进行模型推理
        keypoints,pose_img = body.inference(data=img,img_type='cv2')
        format_result = body.format_output(lang='en')
        if det != []:
            num = pose(img)
            


            position = (50, 100) # 左上角起始点的坐标
            font = cv2.FONT_HERSHEY_SIMPLEX
            scale = 3
            color = (255, 255, 255) # BGR颜色值（白色）
            thickness = 2
            
            if cv2.waitKey(1) & 0xFF == 50:
                sound_ok = 'paimen-ok.wav'
                sound_no = 'paimen-no.wav'
            if cv2.waitKey(1) & 0xFF == 51:
                sound_ok = 'qin-ok.wav'
                sound_no = 'qin-no.wav'
            if cv2.waitKey(1) & 0xFF == 49:
                sound_ok = 'kong.wav'
                sound_no = 'kong.wav'
            # 将文字写入图像中
            if flag_num == num:#手势不变，计数加一
                flag = flag+1
            else:
                flag = 0#否则清零
            flag_num = num
            if flag >=15:#大于30，输出
                
                flag = 0
                pose_num = flag_num
                print(pose_num)

                text = label[pose_num]#文本内容
                cv2.putText(pose_img, text, position, font, scale, color, thickness)
                show_img = pose_img  
                #playsound('paimen-no.wav')
                if pose_num == 0:
                    pygame.mixer.music.load(sound_no)
                    
                    pygame.mixer.music.play()
                    while pygame.mixer.music.get_busy():  # 如果音乐正在播放，则保持循环
                        pygame.time.Clock().tick(10)  # 限制循环的频率，避免CPU过载

                if pose_num == 1:
                    pygame.mixer.music.load(sound_ok)
                    pygame.mixer.music.play()
                    while pygame.mixer.music.get_busy():  # 如果音乐正在播放，则保持循环
                        pygame.time.Clock().tick(10)  # 限制循环的频率，避免CPU过载
                #while pygame.mixer.music.get_busy():
                    #pygame.time.Clock().tick(10)
            cv2.putText(pose_img, text, position, font, scale, color, thickness)
            show_img = pose_img    
        else:
            text = label[2]
            cv2.putText(img, text, position, font, scale, color, thickness)
            show_img = img

        
            # 显示结果图像
        cv2.imshow("Image with Text", show_img)
        if cv2.waitKey(1) & 0xFF == 27:
            break
cap.release()
cv2.destroyAllWindows